/**
 * @author 雷明浩
 * @date 2025/6/3 14:10
 */
public class FindFirstCommonNode {
    // newcoder 两个链表的第一个公共节点
    // https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3FquestionJobId%3D10%26subTabName%3Donline_coding_page
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        ListNode cur1 = pHead1;
        ListNode cur2 = pHead2;
        int size1 = 0, size2 = 0;
        while (cur1 != null) {
            size1++;;
            cur1 = cur1.next;
        }
        while (cur2 != null) {
            size2++;
            cur2 = cur2.next;
        }
        if (size2 > size1) {
            cur1 = pHead2;
            cur2 = pHead1;
        }else {
            cur1 = pHead1;
            cur2 = pHead2;
        }
        for (int i = 0; i < Math.abs(size1-size2); i++) {
            cur1 = cur1.next;
        }
        while (cur1 != null && cur2 != null) {
            if (cur1 == cur2) {
                return cur1;
            }
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
        return null;
    }
}